var hrefList = [];
var titleList = [];

function reverseTitleList() {
    titleList.reverse();
    console.log("Title list has been reversed. Current length:", titleList.length);
}

function downloadHref() {
    var hrefBlob = new Blob([hrefList.join('\n')], { type: 'text/plain;charset=utf-8' });
    var url = URL.createObjectURL(hrefBlob);
    var a = document.createElement('a');
    a.href = url;
    a.download = 'videoList.txt';
    document.body.appendChild(a);
    a.click();

    setTimeout(function() {
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    }, 0);
}

function downloadTitle() {

    var titleBlob = new Blob([titleList.join('\n')], { type: 'text/plain;charset=utf-8' });
    var url = URL.createObjectURL(titleBlob);
    var a = document.createElement('a');
    a.href = url;
    a.download = 'titleList.txt';
    document.body.appendChild(a);
    a.click();

    // 清理
    setTimeout(function() {
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    }, 0);

}

var script = document.createElement('script');
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js';
document.head.appendChild(script);
script.onload = function() {
    function updateHrefList() {
        $('.cover.ld.mask').each(function() {
            var href = $(this).attr('href');
            // Extract the ID (e.g., 661e4e120000000001006e28) from the href
            var titleEach = $(this).siblings('.footer').find('.title span').text();
            var match = href.match(/\/([0-9a-f]{24})\?/);
            if (match && match[1]) {
                var videoId = match[1];
                // Construct the new video URL with the correct format
                var videoUrl = `https://www.xiaohongshu.com/explore/${videoId}`;
                if (hrefList.indexOf(videoUrl) === -1) {
                    hrefList.push(videoUrl);
                }
            }
            if (titleList.indexOf(titleEach) === -1) {
                titleList.push(titleEach);
            }
        });
        console.log(hrefList.length);
    }
    // Execute updateHrefList function every second
    setInterval(updateHrefList, 200);
};

/*
// Bash script for converting MP4 to TS
for f in *.mp4; do
    ffmpeg -i "$f" -c copy -bsf:v h264_mp4toannexb -f mpegts "${f%.mp4}.ts";
done

// Bash commands for combining TS files and converting to MP3
cat *.ts > combined.ts
ffmpeg -i combined.ts -q:a 0 -map a output.mp3
*/
